package EnvioArquivos;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * @author Celso Marigo
 */
public class Cliente {
    
    public static void main(String[] args){
        int port = 2000;
	String server = "localhost";
	Socket socket = null;
	
	DataOutputStream output = null;
	FileInputStream inputStream = null;
	int ERROR = 1;
		
	// connect to server
	try {
	    socket = new Socket(server, port);
	    System.out.println("Conectado ao servidor " +
				   socket.getInetAddress() +
				   ":" + socket.getPort());
	}
	catch (UnknownHostException e) {
	    System.out.println(e);
	    System.exit(ERROR);
	}
	catch (IOException e) {
	    System.out.println(e);
	    System.exit(ERROR);
	}
	
	try {
	
            // Arquivo a ser enviado vai aqui...
            String fname = "c:\\temp\\teste\\cor.dbf";
            inputStream = new FileInputStream(fname);
		
	    output = new DataOutputStream(socket.getOutputStream());
	    long start = System.currentTimeMillis();	    
	    byte[] b = new byte[4096];
	    long read = 0, total = 0;
	    while((read = inputStream.read(b))>=0) {
		total = total + read;
	    	output.write(b);
	    }
	    System.out.println("bytes enviados--"+total+" tempo total--"+(System.currentTimeMillis() - start));
	}
	catch (IOException e) {
	    System.out.println(e);
	}

	try {
		output.close();
	    socket.close();
	    inputStream.close();
	}
	catch (IOException e) {
	    System.out.println(e);
	}
    }
}
